package com.youfang.sixthmedia.ui.weibo;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;

public class ShareSinaWeiboActivity extends SinaWeiboActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.share_weibo_activity);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.share_setting_btn:
					doShareSetting();
					break;

				case R.id.share_send_btn:
					doSentWeibo();
					break;
				case R.id.share_pic:
					doSharePic();
					break;

				}

			}
		};

		for (int id : new int[] { R.id.share_setting_btn, R.id.share_send_btn,
				R.id.share_pic }) {
			this.findViewById(id).setOnClickListener(listener);
		}

		// watch content length,we only accept 140 words.

		EditText content = ((EditText) this.findViewById(R.id.content));
		content.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

				int MAX_SIZE = 140;
				int length = s.length();
				if (length > MAX_SIZE) {
					AndroidUtils.toastShort("只能输入140字");
					((EditText) findViewById(R.id.content)).setText(s
							.subSequence(0, MAX_SIZE));
					return;
				}

				((TextView) findViewById(R.id.words_watch)).setText("还可以输入"
						+ (MAX_SIZE - length) + "字");

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		((TextView) this.findViewById(R.id.title)).setText("新浪微博分享");

		content.setText(String.format("#友房网# #%s# 非常不错，推荐给大家",
				YoufangContext.getCurrentBuilding().buildingname));

		if (this.isAlreadyAuthed()) {
			((TextView) this.findViewById(R.id.bind_status))
					.setText(getNickName());

		}

	}

	private void doShareSetting() {

		this.clearStoreSP();
		this.gotoOAuthActivity();

	}

	private void doSharePic() {

	}

	private void doSentWeibo() {

		String content = ((EditText) this.findViewById(R.id.content)).getText()
				.toString();
		if (StringUtils.isBlank(content)) {
			AndroidUtils.toastShort("分享内容不能为空");
			return;
		}

		// 1.already oauthed
		if (this.isAlreadyAuthed()) {
			sendWeibo(content, null);
			return;
		}

		// 2.not oauthed yet
		this.gotoOAuthActivity();
	}

	@Override
	protected void onOAuthCallback(boolean ok) {
		super.onOAuthCallback(ok);
		if (ok) {

			// refresh nick name
			((TextView) this.findViewById(R.id.bind_status))
					.setText(getNickName());

			// now try to send it again??
			//doSentWeibo();

		}

	}

}
